#include "menu.h"

#define TRANSITION_SPEED 1

Menu::Menu()
{
	_selectedIndex = 0;
	_transIn = false;
	_transOut = false;
}

Menu::~Menu()
{

}
		
void Menu::AddOption(float x, float y, Image* option, Image* option_over)
{
	int index = (int)_options.size();
	_options.push_back(new Sprite());
	
	_options[index]->SetImage(option);
	_options[index]->SetPosition(x, y);
	
	_optionImgs.push_back(option);
	
	_optionOverImgs.push_back(option_over);
	
}

void Menu::MouseOver(Sprite* ptr)
{
	for(int i = 0; i < (int)_options.size(); i++)
	{
		if(_options[i]->CollidesWith(ptr, true))
		{
			for(int j = 0; j < (int)_options.size(); j++)
			{
				_options[j]->SetImage(_optionImgs[j]);
			}
			
			_options[i]->SetImage(_optionOverImgs[i]);
			
			_selectedIndex = i;
			
			return;
		}
	}
}

void Menu::TransitionIn()
{
	_transIn   = true;
	_drift     = 300;
}

void Menu::TransitionOut()
{
	_transOut = true;
	_driftDist = 300;
	_drift     = 0;
}
		
void Menu::SelectNext()
{
	_selectedIndex++;
	
	if(_selectedIndex >= (int)_options.size())
		_selectedIndex = 0;
}


void Menu::SelectPrevious()
{
	_selectedIndex--;
	
	if(_selectedIndex <= 0)
		_selectedIndex = (int)_options.size() - 1;
}

int Menu::GetSelected()
{
	return _selectedIndex;
}

void Menu::Render()
{
	/*if(_transIn)
	{
		if(_drift <= 0)
		{
			_transIn = false;
			for(int i = 0; i < (int)_options.size(); i++)
				_options[i]->Draw();
		}
		else
		{
			for(int i = 0; i < (int)_options.size(); i++)
			{
				//Left/ Right trans
				int t = i % 2;
				
				if(t == 0)
					t = -1;	
				//
				
				_options[i]->Draw(t*_drift, 0);
			}
			
			_drift -= TRANSITION_SPEED;
		}
	}
	else if(_transOut)
	{
		if(_drift >= _driftDist)
		{
			_transOut = false;
		}
		else
		{
			for(int i = 0; i < (int)_options.size(); i++)
			{
				//Left/ Right trans
				int t = i % 2;
				
				if(t == 0)
					t = -1;	
				//
				
				_options[i]->Draw(t*_drift, 0);
			}
			
			_drift += TRANSITION_SPEED;
		}
	}
	else
	{
	*/
		for(int i = 0; i < (int)_options.size(); i++)
			_options[i]->Draw();
	//}
}
